<!-- templates/register.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
    <script>
        function submitForm() {
            const username = document.getElementById('username').value;
            const email = document.getElementById('email').value;

            const data = {
                username: username,
                email: email
            };

            fetch('/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    window.location.href = data.redirect_url;
                } else {
                    alert(data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('An error occurred during registration');
            });

            return false;
        }
    </script>
</head>
<body>
    <h1>Register</h1>
    <form onsubmit="return submitForm();">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>

        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required><br><br>

        <button type="submit">Register</button>
    </form>
    <div id="error-message" style="color: red; display: none;"></div>
</body>
</html>